$(function () {
    $(".content .startTime").jeDate(initjeDate);
    $(".content .endTime").jeDate(initjeDate);
})

function statusBtnClick() {
    var $this = $(this);
    var curStatus = $this.parents("td.table-operate").attr("data-status");
    var status = $this.attr("data-status");
    var id = $this.parents("tr").attr("data-id");
    if (!/\d/.test(status) || !/\d/.test(id)) {
        layer.msg("参数错误！");
        return false;
    }
    if (curStatus == status) {
        layer.msg("更新状态成功!");
        $this.parents("ul.dropdown-menu").slideUp();
        return false;
    }
    var text = "";
    if (status == 1) {
        text = "发布这篇文章";
    } else if (status == 2) {
        if (curStatus == 0 || curStatus == -1) {
            layer.msg("当前状态不允许此操作!", fn);
            $this.parents("ul.dropdown-menu").slideUp();
            return false;
        }
        text = "暂停发布这篇文章";
    } else if (status == -1) {
        text = "放入回收站";
    }
    layer.confirm('您确定要' + text + '吗？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: "../article/back/updateArticleStatus.zb",
            type: "POST",
            dataType: "JSON",
            data: {id: id, status: status}
        }).done(function (data) {
            if (data && data.success) {
                loadArticleList(false);
                layer.msg(data.msg);
            } else {
                layer.msg(data.msg || "操作失败！", function () {
                });
            }
        }).fail(function () {
            layer.msg('可能由于网络等原因操作失败！', function () {
            });
        }).always(function () {
            $this.parents("ul.dropdown-menu").slideUp();
        })
    });
}

function loadArticleList(isLoadPagination) {
    var param = {};
    param.pageNum = global_Art_Var.pageNum;
    param.pageSize = global_Art_Var.pageSize;
    param.title = $("#title").val().trim();
    param.blog_navbar_id = $("#menu").data("id")
    param.status = $("#status").attr("data");
    param.is_demo = $("#is_demo").attr("data");
    param.is_download = $("#is_download").attr("data");
    param.type = $("#type").attr("data");
    param.startTime = $("#startTime").val().trim();
    param.endTime = $("#endTime").val().trim();
    $.ajax({
        url: "../article/back/getArticleAll.zb",
        type: "POST",
        dataType: "JSON",
        contentType: "application/json",
        data: JSON.stringify(param)
    }).done(function (data) {
        var $tbody = $("#article-table tbody");
        $tbody.empty();
        if (data && data.list && data.list.length > 0) {
            var list = data.list;
            for (var i = 0; i < list.length; i++) {
                var l = list[i];
                var tr = "<tr data-id='" + l.id + "'>" +
                    "    <td style='display:none;'>" + l.id + "</td>" +
                    "    <td style='text-align: left;'>" + l.title + "</td>" +
                    "    <td>" + l.blog_navbar_name + "</td>" +
                    "    <td class='hidden-xs'><img src='" + l.front_img + "' height='25' width='40'/></td>" +
                    "    <td style='display: none;'>" + formatCont(l.front_descr, 30) + "</td>" +
                    "    <td>" + l.create_time + "</td>" +
                    "    <td>" + formatDemo(l.is_demo) + "</td>" +
                    "    <td>" + formatDownLoad(l.is_download) + "</td>" +
                    "    <td class='articleStatus'>" + formatStatus(l.status) + "</td>" +
                    "    <td class='table-operate' data-status='" + l.status + "'>" +
                    "        <span class='col-lg-4 col-md-4 col-sm-12 col-xs-12'>" +
                    "           <a class='btn btn-danger dropdown-toggle opt-status' >" +
                    "                  状态<i class='fa fa-sort-desc fa-lg'></i>" +
                    "            </a>" +
                    "            <ul class='dropdown-menu' role='menu' >" +
                    "                 <li role='presentation' >" +
                    "                     <a role='menuitem' tabindex='-1' data-status='1'>立即发布</a>" +
                    "                  </li>" +
                    "                  <li role='presentation'>" +
                    "                      <a role='menuitem' tabindex='-1' data-status='2'>暂停发布</a>" +
                    "                  </li>" +
                    "                  <li role='presentation'>" +
                    "                      <a role='menuitem' tabindex='-1' data-status='-1'>删除</a>" +
                    "                   </li>" +
                    "             </ul>" +
                    "           </span>" +
                    "           <span class='col-lg-4 col-md-4 col-sm-12 col-xs-12'><a class='btn btn-warning' href='../page/article/updateArticle.html?id=" + l.id + "' target='_blank'><i class='fa fa-edit fa-md'></i> 编辑 </a></span>" +
                    "           <span class='col-lg-4 col-md-4 col-sm-12 col-xs-12'><a class='btn btn-primary' href='../frontstage/detail.html?status=preview&id=" + l.id + "&t=" + (new Date().getTime() + 5000) + "' target='_blank'><i class='fa fa-navicon fa-md'></i> 预览 </a></span>" +
                    "    </td>" +
                    "</tr>";
                $tbody.append(tr)
            }
            global_Art_Var.totalPage = data.pages;
            if (isLoadPagination) {
                $(".table-page ul.pagination li").not(".page-next,.page-prev").remove();
                var lis = "";
                for (var i = 0; i < data.pages; i++) {
                    if (i == 0) {
                        lis += " <li class='active'><a href='javascript:void(0)'>" + (i + 1) + "</a></li>";
                    } else {
                        lis += " <li><a href='javascript:void(0)'>" + (i + 1) + "</a></li>";
                    }
                }
                if (global_Art_Var.totalPage === 1) {
                    $(".table-page ul.pagination li.page-next").addClass("disabled");
                    $(".table-page ul.pagination li.page-next").addClass("disabled");
                }
                $(".table-page ul.pagination li.page-prev").after(lis);
            }
        }
    }).fail(function () {
        layer.msg('可能由于网络等原因加载失败！', function () {
        });
    }).always(function () {
        $(".content_loading").hide();
    });
}

function keyDownSearch(e) {
    var keyCode = e.keyCode;
    if (keyCode == 13) {
        global_Art_Var.pageNum = 1;//重置分页
        loadArticleList(true);
        return false;
    }
}

function tableFilterResetClick() {
    var $input = $(".article-panel .panel-body form input");
    $input.removeAttr("data");
    $("#menu").removeData("id");
    $input.val("");
    global_Art_Var.pageNum = 1;//重置分页
    loadArticleList(true);
}

function formatDemo(isDemo) {
    return isDemo == 1 ? "是" : "否";
}

function formatDownLoad(isDownload) {
    return isDownload == 1 ? "是" : "否";
}

//格式化状态，将数字转化为汉字
function formatStatus(status) {
    var desc = "";
    switch (status) {
        case 0 :
            desc = "保存未发布";
            break;
        case 1 :
            desc = "已发布";
            break;
        case 2 :
            desc = "暂停发布";
            break;
        case -1 :
            desc = "回收站";
            break;
        default :
            desc = ""
    }
    return desc;
}

function fn() {
}